package cn.tedu.ivos.repair.service.impl;

import cn.tedu.ivos.repair.mapper.RepairMapper;
import cn.tedu.ivos.repair.pojo.dto.RepairQuery;
import cn.tedu.ivos.repair.pojo.dto.RepairSaveParam;
import cn.tedu.ivos.repair.pojo.entity.Repair;
import cn.tedu.ivos.repair.pojo.vo.RepairVO;
import cn.tedu.ivos.repair.service.RepairService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RepairServiceImpl implements RepairService {
    @Autowired
    RepairMapper repairMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<RepairVO> selectRepair(RepairQuery repairQuery) {
        log.debug("查询维修单业务，参数={}",repairQuery);
        List<RepairVO> repairVOList = repairMapper.select(repairQuery);
        if (repairQuery.getLicense()==null || repairQuery.getLicense().trim().isEmpty()){
            return repairVOList;
        }else {
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setLicense(repairQuery.getLicense());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            List<RepairVO> list = new ArrayList<>();
            for (RepairVO repairVO : repairVOList) {
                for (VehicleVO vehicleVO : vehicleVOList) {
                    if (repairVO.getVehicleId().equals(vehicleVO.getId())) {
                        list.add(repairVO);
                    }
                }
            }
            return list;
        }
    }

    @Override
    public void save(RepairSaveParam repairSaveParam) {
        log.debug("保存维修单业务，参数={}",repairSaveParam);
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);
        if (repair.getId() == null){//新增
            repair.setCreateTime(new Date());
            repairMapper.insert(repair);
            Vehicle vehicle = new Vehicle();
            vehicle.setId(repair.getVehicleId());
            vehicle.setStatus("3");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }else {//更新
            repair.setUpdateTime(new Date());
            repairMapper.update(repair);
            if (repair.getStatus().equals("0")){
                Vehicle vehicle = new Vehicle();
                vehicle.setId(repair.getVehicleId());
                vehicle.setStatus("1");
                vehicle.setUpdateTime(new Date());
                vehicleMapper.update(vehicle);
            }
        }
    }

    @Override
    public void deleteRepairById(Long id, Long vehicleId,String status) {
        log.debug("删除维修单业务，参数={},{},{}",id,vehicleId,status);
        repairMapper.deleteRepairById(id);
        if (status.equals("1")){
            Vehicle vehicle = new Vehicle();
            vehicle.setId(vehicleId);
            vehicle.setStatus("1");
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }
}
